import {
  getMenuList
} from '@/api';
import {
  dictList,
  apiProjectList,
  apiDictMoreList,
  apiPartitionList,
  apiFloornList,
  apiPaysonList,
  apiSystemList
} from '../api/contract';

export default {
  data() {
    //0 草稿 1 审批中 2 审批完成/履行中 3 驳回 4 履行完毕 5 已废止
    return {
      statusList: [{
          dictKey: '0',
          dictValue: '草稿'
        }, {
          dictKey: 1,
          dictValue: '审批中'
        },
        {
          dictKey: 2,
          dictValue: '履行中'
        },
        {
          dictKey: 3,
          dictValue: '驳回'
        },
        {
          dictKey: 4,
          dictValue: '履行完毕'
        },
        {
          dictKey: 5,
          dictValue: '已废止'
        }
      ],
      rightWrongList: [{
        dictKey: 1,
        dictValue: '是'
      }, {
        dictKey: 0,
        dictValue: '否'
      }],
      searchParams: {},
      pickerOptions: {
        disabledDate: time => {
          if (this.searchParams.contractBeginDate) {
            return time.getTime() < this.searchParams.contractBeginDate;
          }
        },
      },
      pickerOptionsStart: {
        disabledDate: time => {
          if (this.searchParams.contractEndDate) {
            return time.getTime() > this.searchParams.contractEndDate;
          }
        },
      },
      projectList:[],
      contractTypeList: [],//合同类型
      partitionList:[],//功能分区
      floorList:[],//楼层
      assetTypeList:[],//资产类型
      paySonList:[],//支出子类
      systemList:[],//设备系统
      systemSonList:[],//设备子系统
      businessTypeList:[],//业务类型
    }
  },
  methods: {
    //去详情 contractStatus: 0 草稿 1 审批中 2 审批完成/履行中 3 驳回 4 履行完毕 5 已废止
    async goPage(item, type) {
      let param = {
        parentId: type == 'first' ? this.$route.query.menuId : this.$route.query.tabId,
      };
      this.getHeaderParam(param, item, type);
      let res = await getMenuList(param);
      let menuUrl = res.rows[0].menuUrl;
      let separator = menuUrl.indexOf('?') > -1 ? '&' : '?';
      let queryData = {
        isRefresh: true,
        isInfo: type == 'draft' ? '' : true,
        parentMenuId: type == 'first' ? this.$route.query.menuId : this.$route.query.tabId,
        menuId: res.rows[0].primaryKey,
        contractKey: item.primaryKey ? item.primaryKey : '',
        contractCode: item.contractCode ? item.contractCode : '',
        contractType: item.contractType,
      };
      this.getHeaderParam(queryData, item, type);

      let paramData = [];
      for (let key in queryData) {
        paramData.push(key + '=' + queryData[key]);
      }

      let href = menuUrl + separator + paramData.join('&');
      window.open(href);
    },
    //区分导航传的参数 
    getHeaderParam(param, item, type) {
      if (item.contractStatus == 0) {
        param.likeDescription = '0'; // 合同管理模块，合同查询模块的'草稿中'合同详情
      } else if (item.contractStatus == 1) {
        param.likeDescription = '2'; // 合同管理模块，合同查询模块的'审批中'合同详情
      } else if (item.contractStatus == 2 || item.contractStatus == 3 || item.contractStatus == 4 || item.contractStatus == 5) {
        if (type == "first") {
          if (item.contractType == 'ZX' || item.contractType == 'GC' || item.contractType == 'TG' || item.contractType == 'XZ' || item.contractType == 'CW' || item.contractType == 'YY') {
            param.likeDescription = '1'; //合同查询模块的执行中，审批完成，驳回，履行完毕，已废止'支出合同'详情
          } else {
            param.likeDescription = '3'; //合同查询模块的执行中，审批完成，驳回，履行完毕，已废止'租赁合同'详情
          }
        } else {
          if (item.contractType == 'ZX' || item.contractType == 'GC' || item.contractType == 'TG' || item.contractType == 'XZ' || item.contractType == 'CW' || item.contractType == 'YY') {
            param.likeDescription = '1a'; //合同管理模块的执行中，审批完成，驳回，履行完毕，已废止'支出合同'详情
          } else {
            param.likeDescription = '1b'; //合同管理模块的执行中，审批完成，驳回，履行完毕，已废止'租赁合同'详情
          }
        }

      }
    },
    //设置当前选择的项目obj
    setProjectInfo(){
      this.projectInfo = this.projectList.filter(item => item.primaryKey == this.searchParams.projectKey)[0]
      console.log(this.projectInfo)
      this.getSystemList()
    },
    //字典
    async getOptionList() {
      let res = await apiDictMoreList({ dictTypeList: ['contractType', 'deviceType'] });
      this.contractTypeList = res.rows[1].dictList || [];
      this.assetTypeList = res.rows[0].dictList || [];
    },
    //功能分区
    async getPartitionList(){
      let res = await apiPartitionList()
      this.partitionList = res.rows || []
    },
    //楼层下拉
    async getFloornList(){
      let res = await apiFloornList({partitionKey:this.searchParams.spacePartition})
      this.floorList = res.rows || []
    },
    //支出子类
    async getPaysonList(){
      let res = await apiPaysonList({bizType:2})
      this.paySonList = res.rows || []
    },
    //设备系统
    async getSystemList(){
      let res = await apiSystemList({spaceKey:this.projectInfo.spaceKey})
      this.systemList = res.rows || []
    },
    //设备子系统
    async getSonSystemList(){
      let res = await apiSystemList({parentPrimaryKey:this.searchParams.system})
      this.systemSonList = res.rows || []
    },
    getStatusName(value) {
      let [obj] = this.statusList.filter(item => item.dictKey == value);
      return obj ? obj.dictValue : '';
    },
    getContractName(value) {
      let [obj] = this.contractTypeList.filter(item => item.dictKey == value);
      return obj ? obj.dictValue : '';
    },
    //账单管理
    billManage() {
      this.$router.push({
        name: "bill-manage"
      })
    },
    //项目信息
    async getProjectList() {
      let res = await apiProjectList();
      this.projectList = res.rows || [];
    },
  },
}
